from django.shortcuts import render

# Create your views here.


from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View


import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib

import hmac
import base64
from hashlib import sha256
import urllib

import datetime

#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from django_test.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from django_test.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

#导入正则
import re

import cv2

import json


from .models import *
from .ser import GoodsSer,UserSer,OrdersSer,CommentSer

import redis


#定义ip和端口
host = 'localhost'
port = 6379

#建立连接
r = redis.Redis(host=host,port=port)




#订单信息
class Order(APIView):
    def get(self, request):
        orders = Orders.objects.all()
        orders_ser = OrdersSer(orders,many=True)
        return Response(orders_ser.data)


#登陆
class Login(APIView):

	def get(self,request):

		#接收参数
		username = request.GET.get('username','null')
		password = request.GET.get('password','null')

		#查询数据
		user = User.objects.filter(username=username,password=password).first()

		if user:
			return Response({
				'code':200,
				'uid':user.id
			})
		else:
			return Response({
			'code':301,
			})


class Comments(APIView):

    def get(self,request):
        uid = request.GET.get("uid",None)
        gid = request.GET.get("gid",None)
        content = request.GET.get("content",None)
        if [uid,gid,content]:
            user = User.objects.filter(id=uid).first()
            goods = Goods.objects.filter(id=gid).first()
            goods.count += 1
            Comment.create(content=content,uid=uid,gid=gid)
            goods.save()
            return Response({'code':200,'message':'恭喜，评论成功'})
        else:
            return Response({'code':301})

